import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login/index.vue'
import Home from '@/views/home/index.vue'
import Layout from '@/views/layout/index.vue'
import Article from '@/views/article/index.vue'
import Publish from '@/views/publish/index.vue'
import Image from '@/views/images/index.vue'
import Comment from '@/views/comment/index.vue'
import Setting from '@/views/setting/index.vue'
import { Message } from "element-ui";
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/',
    component:Layout,
    children:[
      {
        path: '',
        name: 'home',
        component: Home
      },
      {
        path: '/article',
        name: 'article',
        component: Article
      },
      {
        path: '/publish',
        name: 'publish',
        component: Publish
      },
      {
        path:'/image',
        name: 'image',
        component: Image
      },
      {
        path: '/comment',
        name: 'comment' ,
        component: Comment
      },
      {
        path: '/settings',
        name: 'settings',
        component: Setting
      }
    ]
  },
  
]

const router = new VueRouter({
  routes
})


router.beforeEach((to , from ,next) =>{
  console.log(to);
  const userInfo = JSON.parse(window.localStorage.getItem('adminInfo'))
  if(to.path !== "/login" && !userInfo ){
    next("/login")
    Message.error('请先登录！')
  }else{
    next()
    
  }
  next()
})



export default router
